home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3025 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  119 lines

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: problems with a linked list
  5. Date: Thu, 25 Jan 1996 16:57:35 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <31079A5F.2F6C@cmt.lpr.mail.carel.fi>
  8. References: <1996Jan25.125329.23499@dcs.warwick.ac.uk>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Daniel Castillo Molero wrote:
  16. > Dear c community
  17. > I'm having some problems with a small c program which uses pointers
  18. > and would like to know if any of you could give me a hand.
  19. > The following program is intended to create a linked list
  20. > of three integers, inserting each one at the beginning of
  21. > the current list, and then print the three integers,
  22. > but when I run it I get a segmentation fault.
  23. > #include <stdio.h>
  24. > #include <stdlib.h>
  25. > struct side
  26. > {
  27. >   int a;
  28. >   struct side *next;
  29. > };
  30. > struct side *i;
  31. > struct side *first_side;
  32. > struct side *tmp;
  33.  
  34. I would replace first_side and tmp with more logical names:
  35.  
  36.     struct side *head = NULL;
  37.     struct side *tail = NULL;
  38. > /* insert a new element in the list */
  39. > void store_side(struct side *i, struct side *first_side) {
  40. >   if (first_side == NULL) { first_side = i; first_side->next = NULL; }
  41.  
  42. Your first_side = i modifies the _parameter_, not the global first_side.
  43. >   else { i->next = first_side; first_side = i; }
  44. > }
  45.  
  46. I would write this:
  47.  
  48.     void store_side(struct side *i)
  49.     {
  50.         if (head == NULL) { tail = head = i; tail->next = NULL; }
  51.         else { tail = tail->next = i; tail->next = NULL; }
  52.     }
  53. > void main(void) {
  54. > /* insert first element */
  55. > first_side = NULL;
  56. > i = malloc(1*sizeof(struct side));
  57. > (i->a) = 0;
  58. > store_side(i, first_side);
  59.  
  60. Replaced by
  61.  
  62.     store_side(i);
  63. > /* insert second element */
  64. > i = malloc(1*sizeof(struct side));
  65. > (i->a) = 1;
  66. > store_side(i, first_side);
  67.  
  68. Again:
  69.  
  70.     store_side(i);
  71. > /* insert third elemenet */
  72. > i = malloc(1*sizeof(struct side));
  73. > (i->a) = 2;
  74. > store_side(i, first_side);
  75.  
  76. Yet again:
  77.  
  78.     store_side(i);
  79. > /* print the three elements */
  80. > tmp = first_side;
  81. > printf("side: %d \n",tmp->a);
  82. > while (tmp->next != NULL) {
  83. >   tmp = tmp->next;
  84. >   printf("side: %d \n",tmp->a);
  85.  
  86. Now this print stuff would become:
  87.  
  88.     struct side *tmp;
  89.  
  90.     for (tmp = head; tmp; tmp = tmp->next)
  91.         printf("side: %d\n", tmp->a);
  92.  
  93. > }
  94. > }
  95. > --------------------------------------------------
  96. > Any help would be greatly appreciated.
  97. > --
  98. > * Daniel Castillo.  D.C.Molero@dcs.warwick.ac.uk *
  99.  
  100.  
  101. HTH,
  102.  AriL
  103.  
  104. -- 
  105. All my opinions are mine and mine alone.
  106.